﻿using Engine;
using Game;
using GameEntitySystem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TemplatesDatabase;

namespace SCIENEW
{
	public class SubsystemItemBlockBehavior : SubsystemBlockBehavior
	{
		public override int[] HandledBlocks => [BaseItemBlock.Index];

		public Dictionary<string, ItemBehavior[]> behaviorsMap = new();
		public override void Load(ValuesDictionary valuesDictionary)
		{
			base.Load(valuesDictionary);
			foreach (var itemData in ItemsBlockManager.IdMap)
			{
				ItemBlock itemBlock = ItemsBlockManager.GetItem(itemData.Key);
				behaviorsMap.Add(itemData.Key, itemBlock.GetBehaviors(Project));
			}

		}
		public override bool OnUse(Ray3 ray, ComponentMiner componentMiner)
		{
            string deviceBlockID = ItemsBlockManager.GetItemCraftingID(componentMiner.ActiveBlockValue); //获取方块值所对应的设备实例
            ItemBehavior[] behaviors = behaviorsMap[deviceBlockID];
            if (behaviors.Length > 0) {
                foreach (var behavior in behaviors) {
                    return behavior.OnUse(ray, componentMiner);
                }
            }
            return false;
		}

        public override bool OnHitAsProjectile(CellFace? cellFace, ComponentBody componentBody, WorldItem worldItem) {
            string deviceBlockID = ItemsBlockManager.GetItemCraftingID(worldItem.Value); //获取方块值所对应的设备实例
            ItemBehavior[] behaviors = behaviorsMap[deviceBlockID];
            if (behaviors.Length > 0) {
                foreach (var behavior in behaviors) {
                    return behavior.OnHitAsProjectile(cellFace, componentBody, worldItem);
                }
            }
            return false;
        }
    }
}
